package com.dhcc.DRGsHosp.facade.impl;

import java.util.List;
import java.util.Map;

import javax.inject.Named;

import org.openkoala.koalacommons.resourceloader.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.dhcc.DRGsHosp.application.DifficultGroupMonitorApplication;
import com.dhcc.DRGsHosp.facade.DifficultGroupMonitorFacade;
import com.dhcc.DRGsHosp.facade.DrgsIndexCalFacade;
import com.dhcc.DRGsHosp.infra.util.DrgsIndex;
@Named
public class DifficultGroupMonitorFacadeImpl implements DifficultGroupMonitorFacade{

	@Autowired
	private DifficultGroupMonitorApplication diffiGroupApplocation;
	@Autowired
	private DrgsIndexCalFacade drgsIndexCalFacade;
	/**
	 * 
	 *  @Description：疑难病组组数
	 *  @Author ZouYiyu  
	 *  @see com.dhcc.DRGsHosp.facade.DifficultGroupMonitorFacade#getTotalCountDiffi(java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public List<Map<String, Object>> getTotalCountDiffi(String dateFrom, String dateTo, String defalutType,String roleName,String account,String hospCode) {
		// 将时间格式化
		String dateF = DrgsIndex.formateDateFrom(dateFrom);
		String dateT = DrgsIndex.formateDateTo(dateTo);
		String groupScheme=drgsIndexCalFacade.getGroupScheme();
		List<Map<String, Object>> list = diffiGroupApplocation.getDiffiAccount(dateF, dateT, defalutType,groupScheme,roleName,account,hospCode);
		Map<String, Object> map =diffiGroupApplocation.getTotalCountDiffi(defalutType,roleName,account,hospCode);
		list.get(0).put("DIFFIAMOUNT",map.get("DIFFIAMOUNT"));
		return list;
	}

	/**
	 * 
	 *  @Description：院内疑难病组按MDC分布
	 *  @Author ZouYiyu  
	 *  @see com.dhcc.DRGsHosp.facade.DifficultGroupMonitorFacade#getDiffiGroupMdc(java.lang.String)
	 */
	@Override
	public List<Map<String, Object>> getDiffiGroupMdc(String yearCode,String roleName,String account,String hospCode) {
		//获取标杆年份
		String dateF = DrgsIndex.formateDateFrom(yearCode);
		String dateT = DrgsIndex.formateDateTo(yearCode);
		String stdId = drgsIndexCalFacade.getStdID(dateF, dateT);
		String groupScheme=drgsIndexCalFacade.getGroupScheme();
		return diffiGroupApplocation.getDiffiGroupMdc(stdId, yearCode,groupScheme,roleName,account,hospCode);
	}
	/**
	 * 
	 *  @Description：院内疑难病组按MDC下DRG分布
	 *  @Author ZouYiyu  
	 *  @see com.dhcc.DRGsHosp.facade.DifficultGroupMonitorFacade#getDiffiGroupDrg(java.lang.String, java.lang.String)
	 */
	@Override
	public List<Map<String, Object>> getDiffiGroupDrg(String yearCode, String mdcCode,String roleName,String account,String hospCode) {
		// 获取标杆年份
		String dateF = DrgsIndex.formateDateFrom(yearCode);
		String dateT = DrgsIndex.formateDateTo(yearCode);
		String stdId = drgsIndexCalFacade.getStdID(dateF, dateT);
		String groupScheme=drgsIndexCalFacade.getGroupScheme();
		return diffiGroupApplocation.getDiffiGroupDrg(yearCode,stdId,mdcCode,groupScheme,roleName,account,hospCode);
	}
	/**
	 * 
	 *  @Description：删除疑难病组
	 *  @Author ZouYiyu  
	 *  @see com.dhcc.DRGsHosp.facade.DifficultGroupMonitorFacade#delDiffiGroup(java.lang.String)
	 */
	@Override
	public void delDiffiGroup(String drgCode,String roleName,String account,String hospCode) {
		diffiGroupApplocation.delDiffiGroup(drgCode,roleName,account,hospCode);
	}
	/**
	 * 
	 *  @Description：获取疑难病组以外的病组 让用户筛选
	 *  @Author ZouYiyu  
	 *  @see com.dhcc.DRGsHosp.facade.DifficultGroupMonitorFacade#getExtraDiffiGroup(java.lang.String)
	 */
	@Override
	public List<Map<String, Object>> getExtraDiffiGroup(String yearCode,String roleName,String account,String hospCode) {
		// 获取标杆年份
		String dateF = DrgsIndex.formateDateFrom(yearCode);
		String dateT = DrgsIndex.formateDateTo(yearCode);
		String stdId = drgsIndexCalFacade.getStdID(dateF, dateT);
		String groupScheme=drgsIndexCalFacade.getGroupScheme();
		return diffiGroupApplocation.getExtraDiffiGroup(stdId,yearCode,groupScheme,roleName,account,hospCode);
	}
	/**
	 * 
	 *  @Description：添加疑难病组
	 *  @Author ZouYiyu  
	 *  @see com.dhcc.DRGsHosp.facade.DifficultGroupMonitorFacade#addDiffiGroup(java.lang.String)
	 */
	@Override
	public void addDiffiGroup(String drgCode,String account,String hospCode) {
		diffiGroupApplocation.addDiffiGroup(drgCode,account,hospCode);
	}
	/**
	 * 
	 *  setDiffiGroup.  
	 *  @Description：设置疑难病组
	 *  @Author ZouYiyu
	 *  @param yearCode
	 *  @param caseCntPer
	 *  @param mdcWeightPer
	 *  @return 
	 *  @Date:2017年12月7日下午3:20:19
	 */
	@Override
	public int setDiffiGroup(String yearCode, int caseCntPer, int mdcWeightPer,String account,String hospCode) {
		int maxYear=0;
		if(StringUtils.hasLength(yearCode)){
			String[] years=yearCode.split(",");
			if(years.length>0){
				for(String year : years){
					if(Integer.valueOf(year)>maxYear){
						maxYear=Integer.valueOf(year);
					}
				}
				// 获取标杆年份
				String dateF = DrgsIndex.formateDateFrom(maxYear+"");
				String dateT = DrgsIndex.formateDateTo(maxYear+"");
				String stdId = drgsIndexCalFacade.getStdID(dateF, dateT);
				String groupScheme=drgsIndexCalFacade.getGroupScheme();		
				return diffiGroupApplocation.setDiffiGroup(stdId, yearCode,caseCntPer,mdcWeightPer,groupScheme,account,hospCode);
			}else{
				return 0;
			}
		}else{
			return 0;
		}		
	}

}
